package amani.wechat.platform.services.impl;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import amani.wechat.platform.services.ICommonService;
import amani.wechat.platform.services.IShopService; 
import amani.wechat.platform.services.model.HotProduct;
import amani.wechat.platform.services.model.Product;
import amani.wechat.platform.services.model.ProductDescription;
import amani.wechat.platform.services.model.ProductGroup;
import amani.wechat.platform.services.model.WxOrders;

@Service("ShopService")
public class ShopService implements IShopService {

	@Resource(name = "CommonService")
	ICommonService commonService;

	@Override
	public List<Product> getProductsByGroupId(String groupId) {
		List<Map<String, Object>> mapList = commonService
				.loadProductsByGroupId(groupId);
		List<Product> list = new ArrayList<Product>();
		for (int i = 0; i < mapList.size(); i++) {
			Map<String, Object> temp = mapList.get(i);
			Product item = new Product();
			item.setId((Integer) temp.get("id"));
			item.setGroupname((String) temp.get("groupname"));
			item.settype((Integer) temp.get("type"));
			item.setName((String) temp.get("name"));
			item.setCid((Integer) temp.get("cid"));
			item.setPrice((Double) temp.get("price"));
			item.setOriginalcost((Double) temp.get("originalcost"));
			item.setCardId((String) temp.get("cardId"));
			item.setContent((String) temp.get("content"));
			item.setIshot((Boolean) temp.get("ishot"));
			item.setIntro((String) temp.get("intro"));
			item.setMainimageurl((String) temp.get("mainimageurl"));
			item.setSort((Integer) temp.get("sort"));
			item.setSumnum((Integer) temp.get("sumnum"));
			item.setIsopen((Boolean) temp.get("isopen"));
			item.setMinute((Integer) temp.get("minute"));  
			list.add(item);
		}
		return list;
	}
	

	@Override
	public Product getProductById(String produectId) {
		List<Map<String, Object>> mapList = commonService
				.loadProductInfo(produectId);
		List<Product> list = new ArrayList<Product>();
		for (int i = 0; i < mapList.size(); i++) {
			Map<String, Object> temp = mapList.get(i);
			Product item = new Product();
			item.setId((Integer) temp.get("id"));
			item.setGroupname((String) temp.get("groupname"));
			item.settype((Integer) temp.get("type"));
			item.setName((String) temp.get("name"));
			item.setCid((Integer) temp.get("cid"));
			item.setPrice((Double) temp.get("price"));
			item.setOriginalcost((Double) temp.get("originalcost"));
			item.setCardId((String) temp.get("cardId"));
			item.setContent((String) temp.get("content"));
			item.setIshot((Boolean) temp.get("ishot"));
			item.setIntro((String) temp.get("intro"));
			item.setMainimageurl((String) temp.get("mainimageurl"));
			item.setSort((Integer) temp.get("sort"));
			item.setSumnum((Integer) temp.get("sumnum"));
			item.setIsopen((Boolean) temp.get("isopen"));
			item.setMinute((Integer) temp.get("minute"));
			list.add(item);
		}
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<ProductGroup> getProductGroups() {
		List<Map<String, Object>> mapList = commonService.loadProductGroup();
		List<ProductGroup> list = new ArrayList<ProductGroup>();
		for (int i = 0; i < mapList.size(); i++) {
			Map<String, Object> temp = mapList.get(i);
			ProductGroup item = new ProductGroup();
			item.setId((Integer) temp.get("id"));
			item.setName((String) temp.get("name"));
			list.add(item);
		}
		return list;
	}

	@Override
	public void saveOrder(WxOrders orders) {
		int id = commonService.saveWxOrderStep1(
				orders.getIsExpress() == 1 ? true : false, orders.getOrderno(),
				orders.getTotalAmount(), orders.getUseropenid(),
				orders.getMemberno(), orders.getAddress(),
				orders.getUsername(), orders.getPhone(), orders.getCompno(), 
				orders.getCompname(), orders.getCompanyaddress());
		for (int i = 0; i < orders.getWxOrdersDetail().size(); i++) {
			 commonService.saveWxOrderStep2(id,orders.getWxOrdersDetail().get(i).getAmnProductId(), orders.getWxOrdersDetail().get(i).getNumber());
		}

	}
	
	@Override
	public List<ProductDescription> getProductDescriptions(String productId){
		List<Map<String, Object>> mapList = commonService.loadProductDescription(productId);
		List<ProductDescription> list = new ArrayList<ProductDescription>();
		for (int i = 0; i < mapList.size(); i++) {
			Map<String, Object> temp = mapList.get(i);
	       ProductDescription item = new amani.wechat.platform.services.model.ProductDescription();
			item.setImageDescription((String) temp.get("imageDescription"));
			item.setImageurl((String) temp.get("imageurl"));
			list.add(item);
		}
		return list;
	}

}
